package plp.imperative1.declaration;

import java.util.concurrent.Semaphore;

import plp.expressions1.util.Tipo;
import plp.expressions2.expression.ValorConcreto;
import plp.expressions2.memory.AmbienteCompilacao;
import plp.expressions2.memory.VariavelJaDeclaradaException;
import plp.expressions2.memory.VariavelNaoDeclaradaException;

public class Semaforo extends ValorConcreto {

	private Semaphore semaforo;
	
	public Semaforo(int valor) {
		super(valor);
		semaforo = new Semaphore(valor);
	}

	public Tipo getTipo(AmbienteCompilacao amb)
			throws VariavelNaoDeclaradaException, VariavelJaDeclaradaException {
		return Tipo.TIPO_SEMAFORO;
	}

	@Override
	public Object valor() {
		return semaforo.availablePermits();
	}
	
	public void acquire() throws InterruptedException {
		semaforo.acquire();
	}
	
	public void release() {
		semaforo.release();
	}

}
